using CMS.Application.common.Interface;
using CMS.Application.Common.Interface;
using CMS.Application.RequestDto;
using CMS.Domain.Entity;
using Microsoft.AspNetCore.Mvc;
using SoftDemo.Infrastructrue.FileService;

namespace CMS.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    /// <summary>
    /// 点赞个数
    /// </summary>

    public class TagsController : ControllerBase
    {

        private readonly IRepository<BlogCollect> _BlogCollect;
        private readonly IRepository<Users> _Users;
        private readonly IRepository<Blog> _Blog;
        private readonly IRepository<Comment> _Comment;
        private readonly IFileUploadService _FileUploadService;
        public TagsController(IRepository<BlogCollect> BlogCollect, IRepository<Users> Users, IRepository<Blog> Blog, IRepository<Comment> comment, IFileUploadService FileUploadService)
        {
            _BlogCollect = BlogCollect;
            _Users = Users;
            _Blog = Blog;
            _Comment = comment;
            _FileUploadService = FileUploadService;
        }

        //用户点击点赞，发出点赞的文章id和用户的id

        [HttpPost("/tag")]
        public async Task<object> Tag(TagsDto tagsDto)
        {
            //查找是否有该用户
            var ExistUser = _Users.Table.Where(x => x.Id == tagsDto.UserId).ToList().Count();
            if (ExistUser == 0)
            {
                return new
                {
                    code = 400,
                    msg = "用户未登录,无法获取当前用户的信息",
                    data = ""
                };
            }
            //查看是否有该文章
            var ExistBlog = _Blog.Table.Where(x => x.BlogId == tagsDto.BlogId).ToList().Count();
            if (ExistBlog == 0)
            {
                return new
                {
                    code = 200,
                    msg = "不存在该博文,或许该博文以删除!!!",
                    data = ""
                };
            }


            //查看之前在该文章是否有点过赞
            var count = _BlogCollect.Table.Where(x => x.UserId == tagsDto.UserId && x.BlogId == tagsDto.BlogId).ToList().Count();
            if (count != 0)
            {
                return new
                {
                    code = 200,
                    msg = "您已经点赞过了!!!",
                    data = ""
                };
            }
            else
            {
                var tag = new BlogCollect
                {
                    UserId = tagsDto.UserId,
                    BlogId = tagsDto.BlogId
                };
                await _BlogCollect.AddAsync(tag);
                count = _BlogCollect.Table.Where(x => x.UserId == tagsDto.UserId && x.BlogId == tagsDto.BlogId).ToList().Count();
                if (count != 0)
                {
                    return new
                    {
                        code = 200,
                        msg = "点赞成功!!!",
                        data = ""
                    };
                }
                else
                {
                    return new
                    {
                        code = 400,
                        msg = "点赞失败",
                        data = ""
                    };
                }
            }
        }
        /// <summary>
        /// 用户点赞次数->博文
        /// </summary>
        /// <returns></returns>
        // [HttpGet("/GetCountTag/{BlogId}")]
        [HttpPost("/tags")]
        public async Task<object> PostTagsCount(TagsCountDto tagsCountDto)
        {

            var ExistUser = _Users.Table.Where(x => x.Id == tagsCountDto.UserId).ToList();


            if (ExistUser.Count() == 0)
            {
                object[] arr = new object[1];
                arr[0] = new
                {
                    code = "200",
                    msg = "用户未登录,无法获取当前用户的信息",
                    data = ""
                };
                return arr;
            }
            else
            {
                //点赞表上该用户的记录
                var usr = _BlogCollect.Table.Where(x => x.UserId == tagsCountDto.UserId).ToList();
                List<Guid> list = new List<Guid>();
                for (int i = 0; i < usr.Count(); i++)
                {
                    list.Add(usr[i].BlogId);
                }


                // var blogs = _Blog.Table.AsQueryable().OrderBy(time => time.UpdatedAt);
                
                var blogs = _Blog.Table.Where(x => list.Contains(x.BlogId)).AsQueryable().OrderBy(time => time.UpdatedAt);

                //实例化封装后的分页静态类
                var sortPagingFn = new Sortpaging();

                // 调用封装后的分页函数
                var pageArr = await sortPagingFn.SortPaging(blogs, tagsCountDto.page, _FileUploadService, _Users, _BlogCollect, _Comment);



                return new
                {

                    code = "200",

                    data = new
                    {

                        pageArr,

                    },

                    msg = "请求成功"

                };
            }
        }
    }

}